package lvm.view;

import java.util.ArrayList;
import java.util.Map.Entry;

import lvm.Contexte;
import lvm.struct.Sexpr;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class ContextesVariablesView {

	private static ArrayList<ContextesVariablesView> listcvv = new ArrayList<ContextesVariablesView>();
	private static ContextesVariablesView cvv;
	
	private Shell shell;
	private Table table;

	public ContextesVariablesView(String t, Contexte contexte, Display display) {
		shell = new Shell(display,SWT.BORDER | SWT.DIALOG_TRIM);
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		shell.setSize(300, 400);
		shell.setLocation(800, 20);
		shell.setText("Lvm LISP - Variables "+t);

		table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		


		String[] titles = { "Symbole(String)", "Sexpr"  };
		
		
		for (int i = 0; i < titles.length; i++) {
			   TableColumn column = new TableColumn(table, SWT.NONE);
			   column.setText(titles[i]);
		}
		
		for (int i=0; i<titles.length; i++) {
		      table.getColumn (i).pack();
		      table.getColumn(i).setWidth(145);
		}    
		
		shell.open();
		this.refreshVaraibles(contexte);
	}

	public void refreshVaraibles(Contexte contexte) {
		table.removeAll();
		for (Entry<String, Sexpr> currentEntry : contexte.getContexte().entrySet()) {
			String id = currentEntry.getKey();
			String value = currentEntry.getValue().toString();
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, id);
			item.setText(1, value);
		}
		table.redraw();
	}

	
	
	
	public static void initContextesVaraiblesViews(String t,Contexte contexte, Display display){
		cvv = new ContextesVariablesView(t,contexte, display);
	}
	
	public static void initListContextesVaraiblesViews(String t,Contexte contexte, Display display){
		listcvv.add(new ContextesVariablesView(t,contexte, display));
	}

	
	public static void srefreshVaraibles(Contexte contexte) {
		cvv.refreshVaraibles(contexte);
	}

}
